<?php
$script = "
var page_ids = [];
jQuery('.copy-pages').click(function(){
    page_ids = [];
    var titles = [];
    jQuery.each(jQuery('td input.select-on-check:checked'), function(){
        var source_page_id = parseInt(jQuery(this).val());
        page_ids.push(source_page_id);
        
        var title = jQuery(this).closest('td').nextAll('.sort-handle').text();
        if (title.length)
            titles.push(title);
        jQuery(this).attr('checked', false).closest('tr').removeClass('selected');
    });
    jQuery('#check_all').attr('checked', false);
    
    if (page_ids.length <= 0)
    {
        alert('Please choose least one page');
        return false;
    }
    
    jQuery('#target-site .title').html('You select <strong>'+(titles.length > 1 ? titles.length : titles[0])+'</strong> pages to copy');
    
    jQuery.fancybox({
        'autoDimensions' : false,
        'width' : 500,
        'height' : 'auto',
        'padding' : 0,
        'margin' : 0,
        'type' : 'inline',
        'href' : '#target-site',
        'scrolling': 'no',
        'modal': true,
        'titleShow': false
    });
    return false;
});
jQuery('#content').delegate('.actions-column .copy', 'click', function(){
    ".(isset($pageId) ? "var source_page_id = {$pageId};jQuery('#target-site #copy_revision').val(parseInt(jQuery(this).attr('href').replace('#', '')));" : "var source_page_id = parseInt(jQuery(this).attr('href').replace('#', ''));")."
    page_ids = [source_page_id];
    
    var title = jQuery(this).closest('td').prevAll('.sort-handle').text();
    if (title.length)
        jQuery('#target-site .title').html('Copy page \"<strong>'+title+'</strong>\"');
    
    jQuery.fancybox({
        'autoDimensions' : false,
        'width' : 500,
        'height' : 'auto',
        'padding' : 0,
        'margin' : 0,
        'type' : 'inline',
        'href' : '#target-site',
        'scrolling': 'no',
        'modal': true,
        'titleShow': false
    });
    return false;
});
jQuery('#target-site .copy').live('click', function(){
    var url = '".$this->createUrl('/Core/service/ajax', array('SID'=>'Cms.page.copy'))."';
    
    var target_site_id = [];
    jQuery.each(jQuery('#target-site .sites input:checkbox'), function(){
        if (jQuery(this).attr('checked'))
            target_site_id.push(parseInt(jQuery(this).val()));
    });
    
    ".(isset($revision) ? 'page_ids = ['.$pageId.']' : '')."
    
    if (target_site_id.length)
    {
        jQuery.fancybox.close();
        var active_workflow = ".(isset($pageId) ? "jQuery('#active_workflow').val();" : "jQuery('input[name=\"active_workflow\"]').attr('checked') ? 1 : 0;")."
        var link_data = jQuery('input[name=\"link_data\"]').attr('checked') ? 1 : 0;
        var copy_revision = ".(isset($pageId) ? "jQuery('#copy_revision').val();" : "jQuery('input[name=\"copy_revision\"]:checked').val();")."        
        for (var i in page_ids)
        {
            var data = {
                'active_workflow' : active_workflow,
                'link_data' : link_data,
                'copy_revision' : copy_revision,
                'target_site_id' : target_site_id,
                ".(isset($revision) ? "'source_site_id' : {$siteId}," : '')."
                'page_id[]' : page_ids[i]
            };
            jQuery.ajax({
                'async' : false,
                'url' : url,
                'data' : data,
                'success' : function(){
                    //set curret site
                    /*jQuery.get('".$this->createUrl('/Core/service/ajax', array('SID'=>'Cms.site.setCurrent'))."', {'site_id' : target_site_id}, function(){
                        window.location = window.location;
                    });*/                    
                    ".(isset($revision) ? 'parent.jQuery.fancybox.close();' : '')."
                }
            });
        }
        alert('Copy successful.');
    }
    else
    {
        alert('Please choose least one target site.');
    }
    return false;
});
jQuery('#target-site .cancel').live('click', function(){
    ".(isset($revision) ? "parent." : '')."jQuery.fancybox.close();
    return false;
});
";
Yii::app()->clientScript->registerScript(__CLASS__.'#CopySite', $script, CClientScript::POS_READY);
Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.pack.js');
Yii::app()->clientScript->registerCssFile(Yii::app()->theme->baseUrl.'/scripts/fancybox/jquery.fancybox-1.3.4.css');
?>
<style type="text/css">
#target-site {background-color: white;}
div.form .list label {display: inline;}
div.form .list span {width: 160px; display: inline-table;}
</style>
<div<?php echo isset($revision) ? '' : ' style="display: none;"';?>>
    <div id="target-site" class="form">
        <div class="title"><?php echo isset($pageInfo) ? $pageInfo : '';?></div>
        <div class="popup-content">
            <div class="sites row">
                <label>Sites you want to copy to</label>
                <div class="list"><?php echo CHtml::checkBoxList('copy_to_site', '', $sites, array('separator'=>'', 'template'=>'<span>{input} {label}</span>'));?></div>
            </div>
            <?php if (isset($pageId)):?>            
                <?php echo CHtml::hiddenField('copy_revision', isset($revision) ? $revision : 0);?>
                <?php echo CHtml::hiddenField('active_workflow', 1);?>                
            <?php else:?>
            <div class="row">
                <label>Which revisions you want to copy</label>
                <div class="list" style="margin-left: 15px;"><?php echo CHtml::radioButtonList('copy_revision', 'all', array('all'=>'All', 'active'=>'Active revisions only'));?></div>
            </div>
            <div class="row list">
                <?php echo CHtml::checkBox('active_workflow', true);?>
                <?php echo CHtml::label('Automatically set page active in the same workflow (if found)', 'active_workflow');?>
            </div>
            <?php endif;?>            
            <div class="row list">
                <?php echo CHtml::checkBox('link_data', false);?>
                <?php echo CHtml::label('Link instead of copy and separate page content.', 'link_data');?>
            </div>
            <p class="note" style="margin: 0;">Note that required/static widgets are not copied. You will have to manually create widget instances using target site as the widget names.</p>
            <div class="buttons">
                <?php echo CHtml::button('Cancel', array('class'=>'cancel'));?>
                <?php echo CHtml::submitButton('Copy', array('class'=>'copy'));?>
            </div>
        </div>
    </div>
</div>